#include "read_conf.h"


#include "tinyxml.h"


int read_conf(const char* conf_file, configure& conf)
{
	int is_ok = -1;
	try
	{
		TiXmlDocument doc(conf_file);
		bool loadOkay = doc.LoadFile(TIXML_ENCODING_UTF8);
		if ( !loadOkay )
		{
			printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
			return -1;
		}
		char szTmp[1024] = {0};
		TiXmlElement* RootElement = doc.FirstChild("root")->ToElement();
		
		//copyright
		TiXmlElement* MKElement = RootElement->FirstChild("copyright")->ToElement();
		memcpy(szTmp, MKElement->Attribute("name"), strlen(MKElement->Attribute("name")));
		conf.name = szTmp;
		
		memset(szTmp, 0, sizeof(szTmp));
		memcpy(szTmp, MKElement->Attribute("version"), strlen(MKElement->Attribute("version")));
		conf.ver = szTmp;
		
		//mysql
		TiXmlElement* MySqlElement = RootElement->FirstChild("mysql")->ToElement();
		TiXmlElement* SchemasElement = MySqlElement->FirstChild("schemas")->ToElement();
		
		//keyword_schema
		conf.keyword.name = "keyword_schema";
		TiXmlElement* SchemaElement = SchemasElement->FirstChild("keyword_schema")->ToElement();
		TiXmlElement* element = SchemaElement->FirstChild("host")->ToElement();
		conf.keyword.host = element->GetText();
		
		element = SchemaElement->FirstChild("user")->ToElement();
		conf.keyword.user = element->GetText();
		
		element = SchemaElement->FirstChild("password")->ToElement();
		conf.keyword.password = element->GetText();
		
		//url_schema
		conf.url.name = "url_schema";
		SchemaElement = SchemasElement->FirstChild("url_schema")->ToElement();
		element = SchemaElement->FirstChild("host")->ToElement();
		conf.url.host = element->GetText();
		
		element = SchemaElement->FirstChild("user")->ToElement();
		conf.url.user = element->GetText();
		
		element = SchemaElement->FirstChild("password")->ToElement();
		conf.url.password = element->GetText();
		
		
		//pending_schema
		conf.pending.name = "pending_schema";
		SchemaElement = SchemasElement->FirstChild("pending_schema")->ToElement();
		element = SchemaElement->FirstChild("host")->ToElement();
		conf.pending.host = element->GetText();
		
		element = SchemaElement->FirstChild("user")->ToElement();
		conf.pending.user = element->GetText();
		
		element = SchemaElement->FirstChild("password")->ToElement();
		conf.pending.password = element->GetText();
		
		//invalid_schema
		conf.invalid.name = "invalid_schema";
		SchemaElement = SchemasElement->FirstChild("invalid_schema")->ToElement();
		element = SchemaElement->FirstChild("host")->ToElement();
		conf.invalid.host = element->GetText();
		
		element = SchemaElement->FirstChild("user")->ToElement();
		conf.invalid.user = element->GetText();
		
		element = SchemaElement->FirstChild("password")->ToElement();
		conf.invalid.password = element->GetText();
		
		//dict_schema
		conf.dict.name = "dict_schema";
		SchemaElement = SchemasElement->FirstChild("dict_schema")->ToElement();
		element = SchemaElement->FirstChild("host")->ToElement();
		conf.dict.host = element->GetText();
		
		element = SchemaElement->FirstChild("user")->ToElement();
		conf.dict.user = element->GetText();
		
		element = SchemaElement->FirstChild("password")->ToElement();
		conf.dict.password = element->GetText();
		
		
		//threads
		MKElement = RootElement->FirstChild("threads")->ToElement();
		element = MKElement->FirstChild("download_threads")->ToElement();
		conf.download_threads = atoi(element->GetText());
		element = MKElement->FirstChild("keyword_threads")->ToElement();
		conf.keyword_threads = atoi(element->GetText());
		element = MKElement->FirstChild("url_threads")->ToElement();
		conf.url_threads = atoi(element->GetText());
		element = MKElement->FirstChild("pending_threads")->ToElement();
		conf.pending_threads = atoi(element->GetText());
		element = MKElement->FirstChild("invalid_threads")->ToElement();
		conf.invalid_threads = atoi(element->GetText());
		element = MKElement->FirstChild("dict_threads")->ToElement();
		conf.dict_threads = atoi(element->GetText());
		
		//ok
		is_ok = 0;
	}
	catch(...)
	{
	}
	
	
	return is_ok;
}

